package com.ruoyi.basedata.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.basedata.domain.BdMaterial;
import com.ruoyi.basedata.service.IBdMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basedata.domain.OmsSettlementpriceentry;
import com.ruoyi.basedata.mapper.OmsSettlementpriceMapper;
import com.ruoyi.basedata.domain.OmsSettlementprice;
import com.ruoyi.basedata.service.IOmsSettlementpriceService;

/**
 * 结算价格Service业务层处理
 *
 * @author ptt
 * @date 2023-03-07
 */
@Service
public class OmsSettlementpriceServiceImpl implements IOmsSettlementpriceService
{
    @Autowired
    private OmsSettlementpriceMapper omsSettlementpriceMapper;

    @Autowired
    private IBdMaterialService bdMaterialService;

    /**
     * 查询结算价格
     *
     * @param id 结算价格主键
     * @return 结算价格
     */
    @Override
    public OmsSettlementprice selectOmsSettlementpriceById(Long id)
    {
        return omsSettlementpriceMapper.selectOmsSettlementpriceById(id);
    }

    /**
     * 查询结算价格列表
     *
     * @param omsSettlementprice 结算价格
     * @return 结算价格
     */
    @Override
    public List<OmsSettlementprice> selectOmsSettlementpriceList(OmsSettlementprice omsSettlementprice)
    {
        return omsSettlementpriceMapper.selectOmsSettlementpriceList(omsSettlementprice);
    }

    @Override
    public List<Long> selectOmsSettlementpriceIdList(OmsSettlementprice omsSettlementprice)
    {
        return omsSettlementpriceMapper.selectOmsSettlementpriceIdList(omsSettlementprice);
    }

    public List<OmsSettlementprice> selectOmsSettlementpriceAndEntryList(List<Long> ids)
    {
        return omsSettlementpriceMapper.selectOmsSettlementpriceAndEntryList(ids);
    }


    /**
     * 查询结算价格有效期时间列表
     *
     * @param omsSettlementprice 结算价格
     * @return 结算价格
     */
    @Override
    public List<OmsSettlementprice> selectOmsSettlementpriceDate(OmsSettlementprice omsSettlementprice)
    {
        return omsSettlementpriceMapper.selectOmsSettlementpriceDate(omsSettlementprice);
    }

    /**
     * 新增结算价格
     *
     * @param omsSettlementprice 结算价格
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsSettlementprice(OmsSettlementprice omsSettlementprice)
    {
        int i = primecostSuccess(omsSettlementprice,true);
        if(i!=1){
            return i;
        }
        int rows = omsSettlementpriceMapper.insertOmsSettlementprice(omsSettlementprice);
        insertOmsSettlementpriceentry(omsSettlementprice);
        return rows;
    }

    /**
     * 修改结算价格
     *
     * @param omsSettlementprice 结算价格
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsSettlementprice(OmsSettlementprice omsSettlementprice)
    {
        int i = primecostSuccess(omsSettlementprice,false);
        if(i!=1){
            return i;
        }
        omsSettlementpriceMapper.deleteOmsSettlementpriceentryByParentId(omsSettlementprice.getId());
        insertOmsSettlementpriceentry(omsSettlementprice);
        return omsSettlementpriceMapper.updateOmsSettlementprice(omsSettlementprice);
    }

    /**
     * 批量删除结算价格
     *
     * @param ids 需要删除的结算价格主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSettlementpriceByIds(Long[] ids)
    {
        omsSettlementpriceMapper.deleteOmsSettlementpriceentryByParentIds(ids);
        return omsSettlementpriceMapper.deleteOmsSettlementpriceByIds(ids);
    }

    /**
     * 删除结算价格信息
     *
     * @param id 结算价格主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSettlementpriceById(Long id)
    {
        omsSettlementpriceMapper.deleteOmsSettlementpriceentryByParentId(id);
        return omsSettlementpriceMapper.deleteOmsSettlementpriceById(id);
    }

    /**
     * 新增结算价格分录信息
     *
     * @param omsSettlementprice 结算价格对象
     */
    public void insertOmsSettlementpriceentry(OmsSettlementprice omsSettlementprice)
    {
        List<OmsSettlementpriceentry> omsSettlementpriceentryList = omsSettlementprice.getOmsSettlementpriceentryList();
        Long id = omsSettlementprice.getId();
        if (StringUtils.isNotNull(omsSettlementpriceentryList))
        {
            List<OmsSettlementpriceentry> list = new ArrayList<OmsSettlementpriceentry>();
            for (OmsSettlementpriceentry omsSettlementpriceentry : omsSettlementpriceentryList)
            {
                omsSettlementpriceentry.setParentid(id);
                list.add(omsSettlementpriceentry);
            }
            if (list.size() > 0)
            {
                omsSettlementpriceMapper.batchOmsSettlementpriceentry(list);
            }
        }
    }

    /**
     * 新增和修改时检验数据合法性
     *
     * @param omsSettlementprice 结算价格对象
     */
    public int primecostSuccess(OmsSettlementprice omsSettlementprice,boolean isAdd)
    {
        if(StringUtils.isBlank(omsSettlementprice.getPricetactics())){
            //名字为空
            return -4;
        }
        OmsSettlementprice omsSettlementprice1 = new OmsSettlementprice();
        omsSettlementprice1.setPricetactics(omsSettlementprice.getPricetactics());
        List<OmsSettlementprice> omsSettlementprices = omsSettlementpriceMapper.selectOmsSettlementpriceList(omsSettlementprice1);
        if(omsSettlementprices!=null&&omsSettlementprices.size()!=0){
            if(isAdd){
                //名字重复
                return -6;
            }else if(!omsSettlementprice.getId().equals(omsSettlementprices.get(0).getId())){
                //修改时，新名字与数据库已有数据重复，与自己重复时会跳过
                return -6;
            }
        }
        if(omsSettlementprice.getStatus()==0L){
            //未审核时不校验以下数据，正常新增
            return 1;
        }
        if(omsSettlementprice.getOmsSettlementpriceentryList()==null||omsSettlementprice.getOmsSettlementpriceentryList().size()==0){
            //状态为已审核，必须要有分录
            return -7;
        }else{
            List<OmsSettlementpriceentry> omsSettlementpriceentryList = omsSettlementprice.getOmsSettlementpriceentryList();
            for(OmsSettlementpriceentry oms:omsSettlementpriceentryList){
                if(StringUtils.isBlank(oms.getMaterialnumber())||StringUtils.isBlank(oms.getMaterialname())){
                    return -8;
                }else{
                    BdMaterial bdMaterial = new BdMaterial();
                    bdMaterial.setMaterialnumber(oms.getMaterialnumber());
                    bdMaterial.setMaterialname(oms.getMaterialname());
                    List<BdMaterial> bdMaterials = bdMaterialService.selectBdMaterialList(bdMaterial);
                    if(bdMaterials==null||bdMaterials.size()==0){
                        BdMaterial bdMaterial1 = new BdMaterial();
                        bdMaterial1.setMaterialnumber(oms.getMaterialnumber());
                        List<BdMaterial> bdMaterials1 = bdMaterialService.selectBdMaterialList(bdMaterial1);
                        if(bdMaterials1==null||bdMaterials1.size()==0){
                            return -9;
                        }else{
                            oms.setMaterialname(bdMaterials1.get(0).getMaterialname());
                        }
                    }
                }
            }
        }
        Date startdate = omsSettlementprice.getStartdate();
        Date enddate = omsSettlementprice.getEnddate();
        if(startdate==null||enddate==null){
            return -2;
        }
        if(startdate.after(enddate)){
            return -10;
        }
        List<OmsSettlementprice> omsSettlementprices1 = omsSettlementpriceMapper.selectOmsSettlementpriceDate(omsSettlementprice);
        if(omsSettlementprices1!=null&&omsSettlementprices1.size()!=0){
            return -3;
        }
        List<OmsSettlementpriceentry> omsSettlementpriceentryList = omsSettlementprice.getOmsSettlementpriceentryList();
        if(omsSettlementprice.getStatus()==1L&&(omsSettlementpriceentryList==null||omsSettlementpriceentryList.size()==0)){
            return -5;
        }
        HashMap map = new HashMap();
        for(int i = 0;i<omsSettlementpriceentryList.size();i++){
            OmsSettlementpriceentry omsSettlementpriceentry = omsSettlementpriceentryList.get(i);
            if(map.containsKey(omsSettlementpriceentry.getMaterialnumber())){
                return -1;
            }else{
                map.put(omsSettlementpriceentry.getMaterialnumber(),omsSettlementpriceentry.getMaterialname());
            }
            BigDecimal price = omsSettlementpriceentry.getPrice();
            BigDecimal primecostprice = omsSettlementpriceentry.getPrimecostprice();
            BigDecimal joinprice = omsSettlementpriceentry.getJoinprice();
            BigDecimal maintainprice = omsSettlementpriceentry.getMaintainprice();
            if(price==null&&primecostprice==null&&joinprice==null&&maintainprice==null){
                return -11;
            }
        }
        return 1;
    }
}